/************************************************************************************\
This source file is part of the Universal Actor Library                              *
For latest info, see http://libuniactor.sourceforge.net/                             *
**************************************************************************************
Copyright (c) 2010 Kresimir Spes (kreso@cateia.com)                                  *
*                                                                                    *
* This program is free software; you can redistribute it and/or modify it under      *
* the terms of the BSD license: http://www.opensource.org/licenses/bsd-license.php   *
\************************************************************************************/
#include "glSprite2D.h"
#define __DEV_IL
#include "demo_basecode.h"
#include <map>
#include <vector>
#include <fstream>
#include <iostream>
#include <math.h>
#include "gtypes/Vector3.h"

struct FrameInfo
{
	unsigned int texid;
	float x,y,w,h;
};
std::map<std::string,FrameInfo> g_frames;
std::vector<unsigned int> g_textures;

std::string str(int i)
{
    char s[32];
    sprintf(s,"%d",i);
    return std::string(s);
}

void loadSprite(std::string filename)
{
	std::ifstream f;
	std::string name;
	f.open(filename.c_str());
	unsigned int texid=0;
	FrameInfo i;
	
	filename=filename.substr(0,filename.rfind("/"));
	
	if (f.is_open())
	{
		while (!f.eof())
		{
			f >> name;
			if (name.substr(0,1) == ">")
			{
				texid=loadTexture((filename+"/"+name.substr(1,1000)).c_str());
				g_textures.push_back(texid);
				std::cout << "loading " << name.substr(1,1000) << "\n";
			}
			else
			{
				f >> i.x; i.x=(i.x+0.5f)/1024.0f;
				f >> i.y; i.y=(i.y+0.5f)/1024.0f;
				f >> i.w; i.w=(i.w-1.0f)/1024.0f;
				f >> i.h; i.h=(i.h-1.0f)/1024.0f;
				i.texid=texid;
				g_frames[name]=i;
			}
		}
		f.close();
	}
}

glSprite2D::glSprite2D()
{
	mPosition.set(400,400,0);
	mSpeed=100;
	mDirectionString="Stand_S";
	mFrameCounter=1;
}

glSprite2D::~glSprite2D()
{
	
}

void glSprite2D::OnDirectionChanged(gtypes::Vector3 old)
{
	float angle=acos(mDirection.dotProduct(gtypes::Vector3(0,-1,0)))*57.29578f;
	
	if (mDirection.x < 0) angle=360-angle;
	printf("%.1f:",angle);
	std::string orientations[]={"N","NE","E","SE","S","SW","W","NW","N"};
	mDirectionString="Walk_"+orientations[(int) ((angle+22.5)/45)];
	
	printf(" %s\n",mDirectionString.c_str());
	
}

void glSprite2D::OnEndMovement()
{
	if (mDirectionString.substr(0,5) != "Stand")
	{
		mDirectionString="Stand_"+mDirectionString.substr(5,2);
		mFrameCounter=1;
	}
}


void glSprite2D::draw()
{
	std::string framename=mDirectionString+"."+((int) mFrameCounter < 10 ? "0" : "")+str((int) mFrameCounter);
	FrameInfo f=g_frames[framename];
	glBindTexture(GL_TEXTURE_2D,f.texid);

	float scale=1500;
	drawTexturedQuad(mPosition.x-f.w*scale/2,mPosition.y-f.h*scale,f.w*scale,f.h*scale,f.x,f.y,f.w,f.h);
}

void glSprite2D::update(float time_increase)
{
	UniActor::Actor::update(time_increase);
	
	if (mDirectionString.substr(0,5) != "Stand")
	{
		mFrameCounter+=time_increase*15;
		if (mFrameCounter > 16) mFrameCounter=1;
	}
}
